﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Factur_IT.Admin
{
    public partial class addProject : System.Web.UI.Page
    {
        int userID;
        protected void Page_Load(object sender, EventArgs e)
        {
            bool loggedIn = FacturIT.Service.FacturITManager.Instance.IsLoggedID();

            if (loggedIn != true)
                Response.Redirect("~/Account/login.aspx");

            userID = Convert.ToInt32(Session["UserID"]);
            
            if (!IsPostBack)
            {
                foreach (var item in FacturIT.Service.FacturITManager.Instance.ReadAllPaymentTypes())
                {
                    ddlPaymentTypes.Items.Add(new ListItem { Text = item.Name, Value = item.PaymentTypeID.ToString() });
                }

                foreach (var item in FacturIT.Service.FacturITManager.Instance.ReadMyCompanies(userID))
                {
                    ddlMyCompany.Items.Add(new ListItem { Text = FacturIT.Service.FacturITManager.Instance.ReadCompany(item.CompanyID).CompanyName, Value = item.CompanyID.ToString() });
                }

                foreach (var item in FacturIT.Service.FacturITManager.Instance.ReadAllCompanies())
                {
                    ddlInOpdrachtVan.Items.Add(new ListItem { Text = item.CompanyName, Value = item.CompanyID.ToString() });
                }
            }
            
        }

        public void btnSvae_Click(object sender, EventArgs e)
        {
            int projectid;
            int companyid = Convert.ToInt32(ddlMyCompany.SelectedValue);
            string name = txtNaam.Text;
            DateTime start = calStart.SelectedDate;
            DateTime end = calEnd.SelectedDate;
            int paymenttype = int.Parse(ddlPaymentTypes.SelectedItem.Value);
            int customerid = Convert.ToInt32(ddlInOpdrachtVan.SelectedValue);
            FacturIT.Service.FacturITManager.Instance.AddProject(name, start, end, true, paymenttype, customerid);
            projectid = FacturIT.Service.FacturITManager.Instance.GetProjectByValues(name, start, end, true, paymenttype, customerid, companyid).ProjectID;
            FacturIT.Service.FacturITManager.Instance.AddCompanyToProject(projectid, companyid);
        }
    }
}